
/*
 Hermes: General Porpouse Mobile Agent Platform
 Copyright (C) 2004 Diego Bonura, Rosario Culmone, Davide Lorenzoli, 
 Leonardo Mariani, Emanuela Merelli, Francesca Piersigilli.

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

 Emanuela Merelli
 Dipartimento di Matematica ed Informatica
 via Madonna delle Carceri, 62032 Camerino, Italy
 E.Mail: emanuela.merelli@unicam.it
 */
package hermesV2.services;

import hermesV2.Message;

import hermesV2.agent.AbstractProtocol;
import hermesV2.agent.ServiceAgent;
import hermesV2.services.HubService.HubServiceProtocol;
import hermesV2.services.HubService.HandleAgentRequest;
import hermesV2.services.HubService.LoadProperties;
import hermesV2.util.HermesLogger;

import java.util.Properties;

/**
 * @author Administrator To change the template for this generated type comment
 *         go to Window - Preferences - Java - Code Generation - Code and
 *         Comments
 */
public class HubServiceAgent extends ServiceAgent {
       
       private static final String propertyFileName = "/conf/hubServiceAgent.properties";
       private static Properties hubServiceAgentProperty;

       public HubServiceAgent(String app) {
    	   super(app);
       }

       /**
        * The first method will be execute from the Place.
        */
       public void init() {
    	   reception();
       //Registazione servizio
       String[] features = new String[1];
       features[0] = "PeerToPeer";
       registerService(features);
       HubServiceProtocol hubProtocol = new HubServiceProtocol();
       registerProtocol(hubProtocol);
       HermesLogger.log(HermesLogger.INFO, this.getAgentName() + " ON and waiting for new messages!");
       boolean propertyFileOk = true;
       try {

           hubServiceAgentProperty = LoadProperties.loadProperty(this.getPlaceProperties().getProperty("HERMES.HOME_DIRECTORY") + propertyFileName, this.getPlaceProperties().getProperty("HERMES.HOME_DIRECTORY"));
       } catch (Exception e) {
           propertyFileOk = false;
           HermesLogger.log(HermesLogger.WARNING, "Exception during loading JaweServiceAgent property file", e);
           HermesLogger.log(HermesLogger.WARNING, "hubServiceAgent stopped");
       }

       if (propertyFileOk) {

           while (true) {
               Message msg = getMessageSynch();
               new HandleAgentRequest(this, msg, hubProtocol, hubServiceAgentProperty);

           }
       }

   }

}

